/*
 * linkedlist.c
 *
 *      Modified version of: http://codingfreak.blogspot.com/2010/04/singly-linked-list-in-c.html
 */

#include <linkedlist.h>

// Counting number of elements in the List
int32_t linkedlist_length() {
	struct Node *cur_ptr;
	int32_t count=0;

	cur_ptr=Head;

	while(cur_ptr != NULL) {
		cur_ptr=cur_ptr->Next;
		count++;
	}
	return(count);
}

// Deleting a node from List depending upon the data in the node.
bool linkedlist_delNodeData(uint8_t noteID) {
	struct Node *prev_ptr = NULL, *cur_ptr = NULL;

	cur_ptr=Head;

	while(cur_ptr != NULL) {
		if(cur_ptr->Data.noteID == noteID) {
			if(cur_ptr==Head) {
				Head=cur_ptr->Next;
				free(cur_ptr);
				return true;
			}
			else {
				prev_ptr->Next=cur_ptr->Next;
				free(cur_ptr);
				return true;
			}
    	}
		else {
			prev_ptr=cur_ptr;
			cur_ptr=cur_ptr->Next;
		}
	}
	// unable to delete
	return false;
}

// Check if note is already in the list
bool linkedlist_noteAlreadyInList(uint8_t noteID) {
	struct Node *prev_ptr, *cur_ptr;

	cur_ptr=Head;

	while(cur_ptr != NULL) {
		if(cur_ptr->Data.noteID == noteID) {
			return true;
    	}
		else {
			prev_ptr=cur_ptr;
			cur_ptr=cur_ptr->Next;
		}
	}
	// note not in list
	return false;
}

// Adding a Node at the Beginning of the List
void linkedlist_addBeg(note_s note) {
	struct Node *temp;

	temp=(struct Node *)malloc(sizeof(struct Node));
	temp->Data = note;

	if (Head == NULL) {
    	//List is Empty
		Head=temp;
		Head->Next=NULL;
	}
	else {
		temp->Next=Head;
		Head=temp;
	}
}

// peek at element at beginning of list
note_s linkedlist_getBeg(void) {

	return Head->Data;
}

// check if the linked list is empty
bool linkedlist_empty(void) {

	if(Head==NULL) {
		return true;
	}
	return false;
}
